﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DevExpress.Web.ASPxTreeList;
using System.Data.SqlClient;
using DAL;

public partial class SysInfo_RightManager : Frame.Pages.BasePage
{
    protected override string ActionID
    {
        get { return "0"; }
    }

    protected override void Page_Load(object sender, EventArgs e)
    {
        //显示数据treelist
        base.Page_Load(sender, e);
        tvShow();
        userShow();
    }
    //数据查询到treelist中
    void tvShow()
    {
        DataTable dt = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable("select * from css_action");
        this.DxTreelist.DataSource = dt;
        this.DxTreelist.DataBind();
    }
    //绑定用户
    void userShow()
    {
        DataTable dt = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable("select rybm,ryxm from css_user");
        this.ASPxCmbData.DataSource = dt;
        this.ASPxCmbData.DataBind();

    }
    //判断css_user_action表中是否存在当前rybm
    private static bool checkRybm(string bm)
    {
        using (DataClassesDataContext dc = new DataClassesDataContext())
        {
            css_user_action user = dc.css_user_action.FirstOrDefault(v => v.rybm == bm);
            if (user != null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
    //人员授权
    protected void QueryCBPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        //得到从客户端传递过来的参数来进行异步通信响应
        if (e.Parameter.Equals("null"))
        {
            throw new Exception("请选择人员！");
        }
        string right = "";
        //string rights = "";
        if (this.DxTreelist.GetSelectedNodes().Count > 0)   //判断treelist是否勾选了权限信息
        {
            List<TreeListNode> selection = this.DxTreelist.GetSelectedNodes();   //获取选定的权限信息
            //遍历node
            foreach (TreeListNode node in selection)
            {
                List<SqlParameter> Params = new List<SqlParameter>();
                List<SqlParameter> Par = new List<SqlParameter>();
                right = node.GetValue("aid").ToString();
                //rights = rights + node.GetValue("aid").ToString().Substring(4);
                string subRight = right.Substring(4);
                //string sub = right.Substring(0, 3);
                string op_s = "";
                if (subRight.Equals("I") || subRight.Equals("U") || subRight.Equals("D") || subRight.Equals("Q"))
                {
                    //Par.Add(new SqlParameter("@op", rights));
                    //Par.Add(new SqlParameter("@id", sub));
                    //Par.Add(new SqlParameter("@op_s", op_s));
                    //Frame.Global.GlobalSqlHelper.ExcuteSQLScalar("update  css_user_action set op=@op where aid=@id", Params.ToArray());
                }
                if (subRight.Equals("S"))
                {
                    op_s = "Y";
                }
                if (subRight.Equals(" "))
                {
                    //foreach (TreeListNode nodes in selection)
                    //{
                    //    right = right + nodes.GetValue("aid");
                    //}
                }
                //实现修改指定人员的权限信息并保存
                Params.Add(new SqlParameter("@id", e.Parameter));
                Params.Add(new SqlParameter("@right", right));
                Params.Add(new SqlParameter("@op", subRight));
                Params.Add(new SqlParameter("@op_s", op_s));
                //string m = ASPxCmbData.SelectedItem.Text.Substring(0, ASPxCmbData.SelectedItem.Text.IndexOf(";"));
                Frame.Global.GlobalSqlHelper.ExcuteSQLScalar("insert into  css_user_action  (rybm,aid,op,op_s) values (@id,@right,@op,@op_s)", Params.ToArray());
            }

        }
    }
    //权限加载
    protected void DxTreelist_CustomCallback(object sender, DevExpress.Web.ASPxTreeList.TreeListCustomCallbackEventArgs e)
    {
        string[] Rights;
        //选择不同人员后刷新当前人员的权组信息
        if (checkRybm(e.Argument.ToString()) == true)
        {
            for (int i = 0; i < this.DxTreelist.Nodes.Count; i++)
            {
                this.DxTreelist.Nodes[i].Selected = false;
            }
        }
        else
        {
            string right = Frame.Global.GlobalSqlHelper.ExcuteSQLScalar("select aid from css_user_action where rybm =" + e.Argument).ToString();
            Rights = right.Split('|');
            //清除所有选择
            for (int i = 0; i < this.DxTreelist.Nodes.Count; i++)
            {
                this.DxTreelist.Nodes[i].Selected = false;
            }
            //确定是否有权限 显示权限
            if (Rights.Length > 0)
            {
                for (int i = 0; i < Rights.Length; i++)
                {
                    if (!Rights[i].Equals(""))
                    {
                        TreeListNode selection = DxTreelist.FindNodeByKeyValue(Rights[i]);    //查找有权限的项
                        //如果有权限则设置为true
                        if (selection != null)
                        {
                            selection.Selected = true;
                        }
                    }
                }
            }
        }
    }
}